package edu.zut.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * servlet：web容器（tomcat）默认采用的是单实例多线程模式，此时这个count会被用户共享
 * 单实例：多个用户共享这个对象
 * 多实例：每个用户本身使用的是自己的对象
 */
@WebServlet(name = "PVServlet",urlPatterns = "/pv")
public class PVServlet extends HttpServlet {
    //表示当前servlet的访问次数
    int count = 0;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        count++;
        System.out.println("count = " + count);
        response.getWriter().write("<!DOCTYPE html>\n" +
                "<html lang=\"en\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <title>Title</title>\n" +
                "</head>\n" +
                "<body>\n" +
                "用户点击次数" + count +
                "</body>\n" +
                "</html>");

    }
}
